package com.soilflow.info.service.impl;

import java.util.List;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.soilflow.common.domain.DahuProcure;
import com.soilflow.common.domain.DahuProduct;
import com.soilflow.common.utils.DateUtils;
import com.soilflow.info.mapper.DahuProcureMapper;
import com.soilflow.info.mapper.DahuProductMapper;
import com.soilflow.info.service.IDahuProcureService;
import com.soilflow.info.service.IDahuProductService;
import com.soilflow.system.service.IColorfulUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * 【请填写功能名称】Service业务层处理
 * 
 * @author party
 * @date 2023-10-27
 */
@Service
public class DahuProcureServiceImpl extends ServiceImpl<DahuProcureMapper, DahuProcure> implements IDahuProcureService {
    @Resource
    private DahuProcureMapper dahuProcureMapper;
    @Autowired
    private IDahuProductService productService;
    /**
     * 查询【请填写功能名称】
     * 
     * @param id 【请填写功能名称】主键
     * @return 【请填写功能名称】
     */
    @Override
    public DahuProcure selectDahuProcureById(Long id)
    {
        return dahuProcureMapper.selectDahuProcureById(id);
    }

    /**
     * 查询【请填写功能名称】列表
     * 
     * @param dahuProcure 【请填写功能名称】
     * @return 【请填写功能名称】
     */
    @Override
    public List<DahuProcure> selectDahuProcureList(DahuProcure dahuProcure)
    {
        return dahuProcureMapper.selectDahuProcureList(dahuProcure);
    }

    /**
     * 新增【请填写功能名称】
     * 
     * @param dahuProcure 【请填写功能名称】
     * @return 结果
     */
    @Override
    public int insertDahuProcure(DahuProcure dahuProcure)
    {
        dahuProcure.setCreateTime(DateUtils.getNowDate());
        return dahuProcureMapper.insertDahuProcure(dahuProcure);
//        if (procure > 0) {
//            //如果采购成功，则将商品库存改变
//            Long number = dahuProcure.getNumber();
//            LambdaQueryWrapper<DahuProduct> wrapper = new LambdaQueryWrapper<>();
//            wrapper.eq(DahuProduct::getId, dahuProcure.getProductId());
//            DahuProduct product = productService.getOne(wrapper);
//            Long inventory = product.getInventory();
//            product.setInventory(number + inventory);
//            return productService.updateDahuProduct(product);
//        } else {
//            return 0;
//        }
    }

    /**
     * 修改【请填写功能名称】
     * 
     * @param dahuProcure 【请填写功能名称】
     * @return 结果
     */
    @Override
    public int updateDahuProcure(DahuProcure dahuProcure)
    {
        dahuProcure.setUpdateTime(DateUtils.getNowDate());
        if (dahuProcure.getStatus().equals(1)) {
            //如果采购成功，则将商品库存改变
            Long number = dahuProcure.getNumber();
            LambdaQueryWrapper<DahuProduct> wrapper = new LambdaQueryWrapper<>();
            wrapper.eq(DahuProduct::getId, dahuProcure.getProductId());
            DahuProduct product = productService.getOne(wrapper);
            Long inventory = product.getInventory();
            product.setInventory(number + inventory);
        }
        return dahuProcureMapper.updateDahuProcure(dahuProcure);

    }

    /**
     * 批量删除【请填写功能名称】
     * 
     * @param ids 需要删除的【请填写功能名称】主键
     * @return 结果
     */
    @Override
    public int deleteDahuProcureByIds(Long[] ids)
    {
        return dahuProcureMapper.deleteDahuProcureByIds(ids);
    }

    /**
     * 删除【请填写功能名称】信息
     * 
     * @param id 【请填写功能名称】主键
     * @return 结果
     */
    @Override
    public int deleteDahuProcureById(Long id)
    {
        return dahuProcureMapper.deleteDahuProcureById(id);
    }
}
